[Python] pd.read |
您所在的位置:网站首页 › python read csv header › [Python] pd.read |
외부의 csv파일을 python의 dataframe으로 불러올 수도 있고 python으로 만든 dataframe을 csv 파일로 내보낼 수 있다.
import pandas as pd < csv 파일 불러오기 > pandas.read_csv('path/filename.csv', sep=',', delimiter=None, header='infer', names=None, index_col=None, usecols=None, squeeze=False, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, skipfooter=0, nrows=None, na_values=None, keep_default_na=True, na_filter=True, skip_blank_lines=True, quotechar='"', quoting=0, doublequote=True, escapechar=None, encoding=None, error_bad_lines=True, warn_bad_lines=True, delim_whitespace=False)
1. pd.read_csv('경로/불러올파일명.csv') → 같은 폴더에서 불러올 경우 경로 생략 가능 pd.read_csv('경로/파일명.csv')
2. index 지정 index_col : 인덱스로 지정할 열 이름 pd.read_csv('경로/파일명.csv', index_col = '인덱스로 지정할 column명') # Index 지정
3. header 지정 header : 열 이름(헤더)으로 사용할 행 지정 / 첫 행이 헤더가 아닌 경우 header = None pd.read_csv('파일명.csv', header = 1) # header 지정
4. column명 변경 names : 열 이름 지정 pd.read_csv('파일명.csv', names = ['Idx', 'col1', 'col2']) # column명 바꿔서 불러오기
→ header 생략 가능
5. 불러올 행 제한 nrows : 불러올 행 개수 제한 / 처음 ~ n번째 행만 불러오기 skiprows : 처음 ~ n번째 행 제외 / n+1번째 ~ 마지막까지 skipfooter : 뒤에서 n개 제외 pd.read_csv('파일명.csv', nrows = 10) # 처음 ~ n번째 pd.read_csv('파일명.csv', skiprows = 10) # 앞에서 n개 행 생략 pd.read_csv('파일명.csv', skipfooter = 10) # 뒤에서 n개 행 생략1부터 10까지 숫자가 있는 예시 데이터가 있을 때
nrows = 처음 ~ n번째 행까지만 불러온다.
skiprows = 처음 ~ n번째까지 행이 생략되어 n+1번째 ~ 마지막까지 행만 나타난다.
skipfooter = 뒤에서 n개행이 생략된다.
그 외.. sep : 구분자 ex) sep = '\t' usecols : 사용할 열 지정 ex) usecols = ['col1', 'col2'] dtype : data type 설정 na_filter : 결측값 불러올지(True)/말지(False) encoding : 인코딩 -> 한글파일 깨질 때 'UTF-8'로 설정
reference < csv로 내보내기 >예시 데이터 생성 student_card = pd.DataFrame({'ID':[20190103, 20190222, 20190531], 'name':['Kim', 'Lee', 'Jeong'], 'class':['H', 'W', 'S']}) student_card
DataFrame.to_csv(path_or_buf=None, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.', errors='strict')
1. 데이터프레임명.to_csv('경로/저장할 파일명.csv') → 같은 폴더에 저장 시 경로 생략 가능 student_card.to_csv('경로/파일명.csv') # 내보낼 데이터프레임.to_csv('파일명.csv')
2. index 없이 index = False : 인덱스 안 나타나게 student_card.to_csv('파일명.csv', index = False) # index x
3. header 없이 header = False : 열 이름 x student_card.to_csv('파일명.csv', header = False) # header x
4. 인코딩 사용 encoding : 인코딩 지정 student_card.to_csv('파일명.csv', encoding = 'UTF-8') # encoding
reference
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |